<?php

namespace Beerus\Modules\DecorationTie\Actions;

use Encore\Admin\Facades\Admin;
use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;
use Doraemon\model\DesignerDecorationTieClue;
use Doraemon\model\DesignerDecorationTieClueLog;


class ResetStatus extends RowAction
{
    public $name = '重置签约状态';
    const TOAST_ALERT = '确认重置签约状态?';

    public function handle($id)
    {
        $update_data = [
            'sign_status' => DesignerDecorationTieClue::SIGN_STATUS_NONE,
            'sign_time' => null,
            'admin_name' => Admin::user()->username,
        ];
        $tieInfo = DesignerDecorationTieClue::getByCondition([
            'id' => $id,
            'sign_status' => [
                DesignerDecorationTieClue::SIGN_STATUS_SIGNED,
                DesignerDecorationTieClue::SIGN_STATUS_UNSIGNED
            ]
        ], "*");
        if (!$tieInfo || !DesignerDecorationTieClue::updateInfo($update_data, ['id' => $id])) {
            return $this->response()->error('重置签约状态失败');
        } else {
            DesignerDecorationTieClueLog::addInfo([
                'clue_id' => $tieInfo['clue_id'],
                'log' => json_encode($update_data, JSON_UNESCAPED_UNICODE),
            ]);

            return $this->response()->success('重置签约状态成功(*^__^*)')->refresh();
        }
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function dialog()
    {
        $this->confirm(self::TOAST_ALERT);
    }
}
